#include <STC15F2K60S2.H>

sbit key_han_1 = P3^0;
sbit key_han_2 = P3^1;
sbit key_han_3 = P3^2;
sbit key_han_4 = P3^3;

sbit key_lie_1 = P4^4;
sbit key_lie_2 = P4^2;
sbit key_lie_3 = P3^5;
sbit key_lie_4 = P3^4;

static unsigned char key_n[4][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

void Delay20ms()		//@12.000MHz
{
	unsigned char i, j, k;
	i = 1;
	j = 234;
	k = 113;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}


void Timer2Init(void)		//1000微秒@12.000MHz
{
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0xF0;		//设置定时初值
	T2H = 0xF1;		//设置定时初值
	EA = 1;
	IE2 |= 0x04;
	AUXR |= 0x10;		//定时器2开始计时
}

unsigned char Key(void)
{
	unsigned char keynumber = 0;
static unsigned char k_i = 0;
	//P3 |= 0x3f;
	//P3 &= ~(0x01<<k_i);
	if(k_i == 0)P3 = 0xfe;
	if(k_i == 1)P3 = 0xfd;
	if(k_i == 2)P3 = 0xfb;
	if(k_i == 3)P3 = 0xf7;
			if(key_lie_1 == 0) {Delay20ms();while(key_lie_1 == 0);Delay20ms();keynumber = key_n[k_i][0];}
			if(key_lie_2 == 0) {Delay20ms();while(key_lie_2 == 0);Delay20ms();keynumber = key_n[k_i][1];}
			if(key_lie_3 == 0) {Delay20ms();while(key_lie_3 == 0);Delay20ms();keynumber = key_n[k_i][2];}
			if(key_lie_4 == 0) {Delay20ms();while(key_lie_4 == 0);Delay20ms();keynumber = key_n[k_i][3];}
			if(++k_i == 4)
				k_i = 0;
	return keynumber;
}
